const express = require("express"),
  router = express.Router(),
  query = require("../mysql");
router.get("/", async (req, res) => {
  let userList = await query(
    "select userName,userPhone,id,isRegular from user where now() < SUBDATE(loginTime,interval - 24 hour);"
  );
  res.send({ code: 200, data: userList });
});
router.get("/info", async (req, res) => {
  const token = req.headers.authorization.split(" ")[1];
  let uf = await query(
    `select userName,id,userPhone,loginTime from user where token='${token}'`
  );
  if (uf.length > 0) {
    res.send({ code: 200, data: uf[0] });
  } else {
    res.send({ code: 500, data: "无此用户信息" });
  }
});

router.post("/setting", async (req, res) => {
  const { type, sum, once, list } = req.body;
  let queryParams = `select * from drowSetting where id='${1}';`;
  let data = await query(queryParams);
  let params;
  if (!data.length) {
    params = `INSERT INTO drowSetting (type,sum,once) VALUE ('${type}',${sum},${once})`;
  } else {
    params = `UPDATE drowSetting SET type='${type}',sum=${sum},once=${once} where id='${1}';`;
  }
  let setting = await query(params);
  res.send({ code: 200, msg: "设置成功" });
  // if (list) {
  //   const { list } = req.body;
  //   let queryParams = `select * from defaultList where id='${1}';`;
  //   let data = await query(queryParams);
  //   let params;
  //   if (!data.length) {
  //     params = `INSERT INTO defaultList (list) VALUE (${list})`;
  //   } else {
  //     params = `UPDATE defaultList SET list=${list} where id='${1}';`;
  //   }
  //   let setlist = await query(params);
  //   console.log(data);
  // }
});

router.post("/setList", async (req, res) => {
  const { list } = req.body;
  let queryParams = `select * from defaultList where id='${1}';`;
  let data = await query(queryParams);
  let params;
  if (!data.length) {
    params = `INSERT INTO defaultList (list,id) VALUE ('${list}', ${1})`;
  } else {
    params = `UPDATE defaultList SET list='${list}' where id='${1}';`;
  }
  let setlist = await query(params);
  res.send({ code: 200, msg: "设置成功" });
});

router.get("/delList", async (req, res) => {
  await query(`DELETE FROM defaultList where id = 1`);
  res.send({ code: 200, msg: "删除成功" });
});

router.get("/getSets", async (req, res) => {
  let sets = await query("select * from drowSetting");
  let list = await query(`select list from defaultList where id='${1}';`);
  list.length && (sets[0].list = list[0].list);
  res.send({ code: 200, msg: "获取成功", data: sets });
});
router.post("/inputWinnerList", async (req, res) => {
  const param =
    "INSERT INTO winners(`id`,`type`,`userName`,`userPhone`) VALUES ?";
  let data = [];
  req.body.forEach((el) => {
    const { id, type, userName, userPhone } = el;
    data.push([id, type, userName, userPhone]);
  });
  await query(param, [data]);
  res.send({ code: 200, msg: "添加成功" });
});
router.get("/getWinners", async (req, res) => {
  let winners = await query("select * from winners");
  res.send({ code: 200, msg: "获取成功", data: winners });
});
// 向外暴露router
module.exports = router;
